<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        div {
            width: 1200px;
            /* height: 1700px; */
        }
        
        .one {
            height: 400px;
            background-color: burlywood;
            border: 1px solid burlywood;
        }
        
        .two {
            height: 600px;
            top: 402px;
            background-color: pink;
            border: 1px solid pink;
        }
        
        .nav {
            position: relative;
        }
        
        .three {
            height: 300px;
            background-color: lightblue;
            border: 1px solid lightblue;
        }
        
        .four {
            height: 700px;
            background-color: yellow;
            border: 1px solid yellow;
        }
        
        .sidebar {
            width: 50px;
            height: 200px;
            background-color: violet;
            position: absolute;
            top: 450px;
            right: 2px;
        }
        
        .tops {
            width: 40px;
            height: 40px;
            background-color: #777;
            position: fixed;
            bottom: 20px;
            right: 5px;
        }
        
        span {
            display: none;
            position: absolute;
            bottom: 0px;
        }
    </style>
</head>

<body>
    <div id="box">
        <div class="sidebar"><span>返回顶部</span></div>
        <div class="one"></div>

        <div class="nav">
            <div class="two"></div>

        </div>
        <div class="three"></div>
        <div class="four"></div>
    </div>
    <div class="tops">

    </div>
    <script>
        function animate_2(obj, target) {
            clearInterval(obj.timer); //pi'mia避免一个obj绑定多个定时器

            obj.timer = setInterval(function() { //给具体obj绑定定时器 节省空间
                // var step = Math.ceil((target - obj.offsetLeft) / 10);
                var step = (target - window.pageYOffset) / 10;
                step > 0 ? Math.ceil(step) : Math.floor(step);
                if (window.pageYOffset == target) {
                    clearInterval(obj.timer);
                }
                // obj.style.left =  window.pageYOffset + step + 'px';
                window.scroll(0, window.pageYOffset + step);

            }, 30);
        }
        var sidebar = document.querySelector(".sidebar");
        var nav = document.querySelector(".two")
        var bnnerTop = nav.offsetTop;
        var sbTop = sidebar.offsetTop;
        console.log(sbTop);
        console.log(bnnerTop);
        var span = document.querySelector("span");

        document.addEventListener('scroll', function() {
            if (window.pageYOffset >= 402) {
                sidebar.style.position = 'fixed';
                sidebar.style.top = (sbTop - 402) + 'px';
                if (window.pageYOffset >= 702) {
                    span.style.display = 'block';
                    span.addEventListener('click', function() {
                        // window.scroll(0, 10);
                        animate_2(window, 0);
                        span.style.display = 'none';
                    })


                }
            } else {
                sidebar.style.position = 'absolute';
                sidebar.style.top = 450 + 'px';
                // alert(11);

            }

        })
    </script>
</body>

</html>